package com.psa.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.psa.adapter.InterchangeListAdapter;
import com.psa.element.Interchange;
import com.psa.system.R;
import com.psa.tool.DBTool;
import com.psa.tool.SystemStatus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by david on 2014-11-24.
 */
public class InterchangeDialog extends DialogManager implements View.OnClickListener {
    private Activity parentActivity;
    private Dialog dialog;
    private View layout;
    private ListView lv_interchange;
    private List<Interchange> interchangeList;
    private InterchangeListAdapter adapter;
    private OnInterchangeItemClickListener onInterchangeItemClickListener;

    public void setOnInterchangeItemClickListener(OnInterchangeItemClickListener onInterchangeItemClickListener) {
        this.onInterchangeItemClickListener = onInterchangeItemClickListener;
    }

    public interface OnInterchangeItemClickListener{
        void onItemClick(InterchangeDialog dialog, Interchange interchange);
    }

    public InterchangeDialog(Activity activity) {
        parentActivity = activity;
        initView();
    }

    @Override
    public View initView() {
        layout = parentActivity.getLayoutInflater().inflate(R.layout.interchange_dialog_layout, null);
        lv_interchange = (ListView)layout.findViewById(R.id.lv_interchanges);
        lv_interchange.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                adapter.setIndex(i);
                if (onInterchangeItemClickListener!=null)
                    onInterchangeItemClickListener.onItemClick(InterchangeDialog.this, interchangeList.get(i));
                dialog.dismiss();
            }
        });
        ((Button)layout.findViewById(R.id.btn_close)).setOnClickListener(this);
        tv_title = (TextView)layout.findViewById(R.id.tv_title);
        interchangeList = new ArrayList<Interchange>();
        refreshInterchangeList();
        adapter = new InterchangeListAdapter(parentActivity, interchangeList);
        lv_interchange.setAdapter(adapter);
        title = SystemStatus.MainActivity.getString(R.string.inter_dia_code_title);
        return layout;
    }

    @Override
    public void onClick(View view) {
        if (dialog!=null)dialog.dismiss();
    }

    private void refreshInterchangeList(){
        String sql = "Select * From Interchange";
        Cursor cursor = DBTool.getInstance().query(sql);
        interchangeList.clear();
        if (cursor != null) {
            Interchange interchange = new Interchange();
            while ((interchange = (Interchange)interchange.full(cursor)) != null){
                interchangeList.add(interchange);
            }
        }
    }
}
